package com.springBoot.springBootSysCore.common.service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.springBoot.springBootSysCore.modules.services.system.SysFileTmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;

import com.springBoot.springBootSysCore.common.repository.IBaseRepository;
import com.springBoot.springBootSysCore.common.service.jpaQuery.DynamicSpecifications;
import com.springBoot.springBootSysCore.common.service.jpaQuery.IWebUtils;
import com.springBoot.springBootSysCore.common.service.jpaQuery.SearchFilter;
import com.springBoot.springBootSysCore.common.utils.returnUtil.CodeEnum;
import com.springBoot.springBootSysCore.common.utils.returnUtil.ReTurnAppConfig;
import com.springBoot.springBootSysCore.common.utils.returnUtil.ResultJsonFormat;
import org.springframework.stereotype.Service;

//@Transactional(readOnly=true)
public class IBaseServiceImpl<T, PK extends Serializable> implements IBaseService<T, PK>{

	@Autowired
	public SysFileTmpService sysFileTmpService;
	@Autowired
	private IBaseRepository<T, PK> repository;
	@Override
	public T findById(PK id) {
		return repository.findOne(id);
	}
//	@Override
//	public T findOne(T t) {
//		return  t;
//		return repository.findOne(getSpecification(andParam, orParam));
//	}
	@Override
	public T find(Specification<T> specification) {
		return repository.findOne(specification);
	}
	@Override
	public List<T> findAll() {
		return repository.findAll();
	}
	@Override
	public Map<String, Object> delete(PK id) {
		ResultJsonFormat ret = new ResultJsonFormat("删除成功", CodeEnum.SUCCESS.value());
		try {
			repository.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			ret.setMsg("删除失败");
			ret.setFlag(CodeEnum.ERROR.value());
		}
		return ret.convertResultJson();
	}
	@Override
	public Map<String, Object> save(T t) {
		ResultJsonFormat ret = new ResultJsonFormat("保存成功", CodeEnum.SUCCESS.value());
		try {
			repository.saveAndFlush(t);
		} catch (Exception e) {
			e.printStackTrace();
			ret.setMsg("保存失败");
			ret.setFlag(CodeEnum.ERROR.value());
		}
		return ret.convertResultJson();
	}
	@Override
	public T saveEntiey(T t) {
		T newT = null;
		try {
			newT  = repository.saveAndFlush(t);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return newT;
	}
	@Override
	public Page<T> getPage(HttpServletRequest request, Pageable pageable) {
		Map<String, Object> andParams = IWebUtils.getParametersStartingWith(request, ReTurnAppConfig.SEARCH_KEY);
		return getPage(andParams, pageable);
	}
	@Override
	public Page<T> getPage(Map<String, Object> andParams, Pageable pageable) {
		return getPage(andParams, null, pageable);
	}
	@Override
	public Page<T> getPage(Map<String, Object> andParam, Map<String, Object> orParam, Pageable pageable) {
		return repository.findAll(getSpecification(andParam, orParam), pageable);
	}
	@Override
	public  Specification<T> getSpecification(Map<String, Object> andParam, Map<String, Object> orParam) {
		Collection<SearchFilter> andfilters = null;
		if(andParam!=null){
			andfilters = SearchFilter.parse(andParam).values();
		}
		Collection<SearchFilter> orfilters = null;
		if(orParam!=null){
			orfilters = SearchFilter.parse(orParam).values();
		}
		Specification<T> specification = DynamicSpecifications.bySearchFilter(andfilters, orfilters);
		return specification;
	}
	@Override
	public List<T> findAll(Specification<T> specification) {
		if(specification==null){
			return repository.findAll();
		}
		return repository.findAll(specification);
	}
	@Override
	public List<T> findAll(Map<String, Object> param) {
		return repository.findAll(getSpecification(param, null));
	}
	@Override
	public Page<T> getPage(Specification<T> specification, Pageable pageable) {
		return repository.findAll(specification, pageable);
	}

	/**
	 * 字段重复检验 -如果返回空则通过
	 * @param dbIds 根据字段从数据库查出的id集合
	 * @param id 当前id
	 * @param rj 返回ResultJsonFormat对象
	 * @return
	 */
	public static  Map<String, Object> crosschecking(List<String> dbIds,String id,ResultJsonFormat rj,String msg){
		if(dbIds!=null && !dbIds.isEmpty()){
			if(id!=null){
				if(dbIds.size()>1){
					rj.setMsg(msg);
					rj.setFlag(CodeEnum.Failed.value());
					return rj.convertResultJson();
				}
				for(String dbId:dbIds){
					if(!dbId.equals(id)){
						rj.setMsg(msg);
						rj.setFlag(CodeEnum.Failed.value());
						return rj.convertResultJson();
					}
				}
			}
		}
		return null;
	}

	/**
	 * 字段重复检验
	 * @param dbIds
	 * @param id
	 * @return
	 */
	public static boolean crosschecking(List<String> dbIds,String id){
		if(dbIds!=null && !dbIds.isEmpty()){
			if(id!=null){
				if(dbIds.size()>1){
					return false;
				}
				for(String dbId:dbIds){
					if(!dbId.equals(id)){
						return false;
					}
				}
			}
		}
		return true;
	}

}
